#!/bin/bash

## hyphop ##

#= build demo openwrt khadas vims image

## HOW TO

## ./build -1|-2|-3|-3l

PR=$(dirname $0)
PA=$(realpath $PR)

. $0.lib
. $0.conf

case $PATH in
    */opt/bin:*);;
    *)PATH="$PA:$PA/../opt/bin:$PA/../../khadas-rescue-tools/image:$PATH";;
esac


APPS=""
apps=""

ITYPE=sd

#MAKE_KRESQ_IMAGE=1
#NO_REMOVE_SQUASH=1

for p in $@; do

    case $p in
	-r|-rr)

	echo "[i] force refresh">&2

	[ -d $OPKG_LIST ] && {
	    CMD rm -rf $OPKG_LIST
	}

	[ -f $OPKG_CONF.$REL ] && {
	    CMD rm $OPKG_CONF.$REL
	}

	[ "$p" = "-rr" ] && {
	    echo "[i] clean opkg cache">&2
	    CMD rm -rf "$PR/../bin/opkg-cache"
	    CMD mkdir -p "$PR/../bin/opkg-cache"
	}

	;;
	mmc|emmc)
	ITYPE=emmc
	;;
	sd)
	ITYPE=sd
	;;
	-rel=*)
	## REL
	export REL=${p#*=}
	echo "[i] force REL: $REL">&2
	;;
	-vim2|-2|-VIM2)
	BOARD=VIM2
	;;
	-vim3|-3|-VIM2)
	BOARD=VIM3
	;;
	-vim3l|-3l|-VIM3L)
	BOARD=VIM3L
	;;
	-vim1|-1|-VIM1|-VIM|-vim)
	BOARD=VIM1
	;;
	-Edge|-e|-EDGE)
	BOARD=Edge
	;;

	'+'*)
	A="${p#+}"
	APPS="$APPS $A"
	;;

	*)
	echo "[w] unknown param $p">&2
	exit 1
	;;
    esac

done

echo "[i] config board: $BOARD rel : $REL // apps:$APPS">&2

IMG_EXT4_NO=1

#[ "$apps" ] || apps=base

apps=$(echo $APPS)
apps="${apps// /.}"

#echo $apps
#exit 0

OUT_=/tmp

IMG_CNAME_="$Label.$REL.$apps.$ITYPE"
IMG_CNAME="$Label.$REL.$apps.$ITYPE.$VER"

IMG_CNAML_="$Label.$apps.$ITYPE"
IMG_CNAML="$Label.$apps.$ITYPE.$VER"

IMG_EXT4="$OUT_/$BOARD.$Label.$REL.$VER.$apps.ext4.$ITYPE.img"

IMG_VFAT="$OUT_/$BOARD.$IMG_CNAME.img"

IMG_NAME1=Khadas.$Label.$REL.$apps
IMG_NAME2=Khadas.$Label.$REL.$apps.$VER

IMG_="$OUT_/$IMG_NAME2"
IMG_LAST="$OUT_/Khadas.$Label.$apps.last"

## check
REQUIRED="rsync curl wget sync sfdisk gzip mkimage mksquashfs mkfs.ext4 mkfs.vfat mdir mcopy"

echo "[i] check required: $REQUIRED">&2

for t in $REQUIRED ; do
    c=$(which $t) || {
	echo "[e] $t not installed">&2
	exit 1
    }
done

#echo "[i] check sfdisk">&2
#sfdisk -v

sfdisk=$(which sfdisk)
[ "$sfdisk" ] || FAIL sfdisk

mksquashfs=$(which mksquashfs)
[ "$mksquashfs" ] || FAIL mksquashfs

BIN="$PR/../bin"

[ -d $BIN ] || \
CMD mkdir -p $BIN

echo "[i] build $REL / $TARGET ">&2

R=
DL() {

    echo "[i] DL $@">&2

    F=$(basename $1)
    T=$2
    [ "$T" ] || T=$BIN
    S="$T/$F"
    R=
    [ -f "$S" ] && {

    [ -s "$S.md5sum" ] && {

	echo "[i] $F already downloaded">&2
	R=$S
	return

    }

	CMD rm $S
	
    }

    [ -f "$S" ] || {
	case "$DL_PROG" in
	    curl)
#	    CMD curl -f -jkL "$1" -o "$S" -#

    [ "$MAXTRY" ] || \
        MAXTRY=21
    [ "$DL_MIN_SPEED" ] || \
        DL_MIN_SPEED=10000
    [ "$DL_MIN_SPEED_TIME" ] || \
        DL_MIN_SPEED_TIME=3

[ "$OPKG_CURL_SILENT" ] && \
    OPKG_CURL_ARGS="-s -S"

	    CMD curl -f -jkL "$1" -o "$S" \
		-Y $DL_MIN_SPEED -y $DL_MIN_SPEED_TIME  \
		--retry $MAXTRY --retry-delay 3

	    curl_code=$?
	    [ "$curl_code" = "0" ] || {
	        echo "[w] curl exit $curl_code :: $1">&2
		[ -f $S ] && rm "$S"
		return 1
	    }
	    ;;
	    *)
	    CMD wget "$1" -O"$S" || return 1
	    ;;
	esac
	R=$S
    }

    [ -s "$S.md5sum" ] || {
	CMD md5sum $S | tee $S.md5sum
    }

}

T=
UNPACK(){
    T=
    case $1 in
	*.tar.*)
	T=${1%.tar.*}

	[ -d "$T" ] || return 0

	mkdir "$T"

	CMD tar -C "$T" -xf  "$1"
	;;
	*)
	echo "[i] undef format $1">&2
	return 1
    esac
}

## PREPRARE HOST OPKG

DL https://downloads.openwrt.org/releases/$REL/targets/x86/64/openwrt-$REL-x86-64-generic-rootfs.tar.gz || FAIL

#HOST_ARCH="$(uname -m)"

TO="$PR/../bin/$HOST_ARCH"
[ -d "$TO" ] || \
CMD mkdir -p $TO
CMD tar -xf $R -C "$TO" ./bin/opkg  ./lib/libc.so  ./lib/libgcc_s.so.1  ./lib/libubox.so

###

# cd ../bin
# DL http://downloads.openwrt.org/releases/18.06.4/packages/x86_64/base/opkg_2019-06-14-dcbc142e-1_x86_64.ipk || FAIL

DL https://downloads.openwrt.org/releases/$REL/targets/$TARGET/64/openwrt-$REL-$TARGET-64-default-rootfs.tar.gz || FAIL

#UNPACK $R
#OWBASE=$T
OWBASE_TAR=$R

#exit 0


echo "$OWBASE_TAR"

#exit 

#OWTMP=/tmp/openwrt_khadas

[ "$OWTMP" ] || FAIL OWTMP undefined

rm -rf $OWTMP

[ -d "$OWTMP" ] || mkdir -p $OWTMP

#https://github.com/hyphop/khadas-linux-kernel/releases/download/openwrt/vims.dtb.tar.gz

for k in \
    Image.gz \
    fdt.tar.gz; do
    DL $KERNEL_DL/$KERNEL_REL/$k || FAIL
done

CMD mkdir $OWTMP/boot

CMD tar -xf $R -C $OWTMP/boot || FAIL

for k in \
    modules.openwrt.tar.gz; do
    DL $KERNEL_DL/$KERNEL_REL/$k || FAIL
done

#UNPACK $R 
MODULES_TAR=$R

#DL https://github.com/hyphop/khadas-uboot-spi/releases/download/$UBOOT_REL/u-boot.$BOARD.sd.bin || FAIL
DL $UBOOT_DL/$UBOOT_REL/$BOARD.u-boot.sd.bin || FAIL
UBOOT_BIN=$R

#DL https://github.com/hyphop/pkg/releases/download/openwrt_khadas_fw/openwrt_khadas_fw.tar.gz || FAIL
#DL https://github.com/hyphop/pkg/releases/download/openwrt_khadas_fw_vims/openwrt_khadas_fw_vims.tar.xz || FAIL
DL https://github.com/hyphop/pkg/releases/download/wlan-firmware-khadas/openwrt_khadas_fw_vims.tar.xz || FAIL

FW_BIN=$R

#CMD cp -a $OWBASE/* $OWTMP
CMD tar -xf $OWBASE_TAR -C $OWTMP

CMD rm -rf $OWTMP/lib/modules/*
CMD tar -xf $MODULES_TAR -C $OWTMP || FAIL
CMD tar -xf $FW_BIN -C $OWTMP || FAIL

TMPMOD=/tmp/

#for RRR in $REP_MOD; do
#    echo "[i] block original modules $RRR">&2
#    CMD ln -s $TMPMOD $OWTMP/lib/modules/$RRR
#done

for RRR in $OWTMP/lib/modules/4*; do
    [ -d "$RRR" ] && {
    echo "[i] block original modules $RRR">&2
    CMD ln -s $TMPMOD $RRR
    }
done

[ -d $TMPMOD ] || mkdir $TMPMOD

#exit 0

#CMD mkdir $OWTMP/boot

ENTER_ADDR=0x01080000
ENTER_ADDR=0x0f080000

CMD mkimage -A $TARGET_KERNEL_ARCH \
    -O linux -C gzip -T kernel -a $ENTER_ADDR -e $ENTER_ADDR \
    -n kernel -d $PR/../bin/Image.gz $OWTMP/boot/uImage.gzip

#CMD cp $PR/../bin/*.dtb $OWTMP/boot
CMD cp $UBOOT_BIN $OWTMP/boot
CMD cp $PR/../files/splash/splash.bmp.gz $OWTMP/boot
CMD cp $PR/../files/splash/splash.bgra.bmp.gz $OWTMP/boot
CMD cp $PR/../files/splash/splash.bgra.bmp.gz $OWTMP/splash.bmp

echo $LABEL > $OWTMP/$LABEL.label
echo $LABEL > $OWTMP/LABEL

CMD cp $PR/../files/user.env $OWTMP/boot/user.env

#echo cmdline= > $OWTMP/boot/user.env

$PR/post_install || FAIL

#CMD rsync -a --inplace $PR/../files/root/* $OWTMP
CMD rsync -a --inplace $PR/../files/root/. $OWTMP

for a in $APPS; do
    [ -d $PR/../files/root.$a ] && {
	echo "[i] copy files for $a">&2
#	CMD rsync -a --inplace $PR/../files/root.$a/* $OWTMP
	CMD rsync -a --inplace $PR/../files/root.$a/. $OWTMP
    }
done

for a in $APPS; do
    [ -f $PR/post_install_$a ] && {
    echo "[i] add apps $a">&2
    $PR/post_install_$a || FAIL
    }
done

## INSTALL EXTRA openwrt packages

DL https://github.com/hyphop/pkg/releases/download/bins/openwrt_extra_bins.tar.gz || FAIL

CMD tar -xf $R -C $OWTMP || FAIL

$PR/post_install.services || FAIL

for a in $APPS; do
    [ -f $PR/post_install_$a.services ] && {
    echo "[i] add apps $a">&2
    $PR/post_install_$a.services || FAIL
    }
done


## FAILSAFE
[ "$NO_FAILSAFE" ] && {
    echo "[i] disable failsafe | preinit ">&2
#    sed -i s/pi_preinit_no_failsafe=\"\"/pi_preinit_no_failsafe=\"1\"/ $OWTMP/lib/preinit/00_preinit.conf
#    chmod 0644 $OWTMP/etc/preinit

[ "" ] && {
    mv $OWTMP/etc/preinit $OWTMP/etc/preinit.disabled

echo "#!/bin/sh
## hyphop ##
#= preinit disabled
" > $OWTMP/etc/preinit

    mv $OWTMP/lib/preinit $OWTMP/lib/preinit.disabled
}

    CMD cp -a $PR/../files/preinit/* $OWTMP

}

# clean wrong kernel modules

#CMD rm -rf $OWTMP/lib/modules/4*

DATE="$(TZ= date)"

echo "[i] BUILD: $LABEL $REL $VER - $DATE" | tee $OWTMP/etc/banner.mod

CMD mkimage -C none -A arm -T script -d $PR/../files/openwrt_ext4.cmd $OWTMP/s905_autoscript
CMD cp $OWTMP/s905_autoscript  $OWTMP/aml_autoscript
CMD cp $OWTMP/s905_autoscript  $OWTMP/boot.scr.uimg
CMD cp $PR/../files/openwrt_ext4.cmd $OWTMP/boot.cmd

cat $PR/../files/boot.ini \
    $PR/../files/openwrt_ext4.cmd > $OWTMP/boot.ini

[ "$IMG_EXT4_NO" ] || {

CMD dd if=/dev/zero bs=1M count=$PART1SIZE of=$IMG_EXT4

CMD dd if=$UBOOT_BIN of=$IMG_EXT4 conv=fsync,notrunc bs=444 count=1
CMD dd if=$UBOOT_BIN of=$IMG_EXT4 conv=fsync,notrunc bs=512 skip=1 seek=1

echo "label:dos" | $sfdisk -q $IMG_EXT4 || FAIL
echo "$P1OFFSET, " | $sfdisk -q $IMG_EXT4 -a || FAIL

CMD mkfs.ext4 -F -L ${LABEL} -d $OWTMP -E offset=$((P1OFFSET*512)) $IMG_EXT4 || FAIL
CMD sync

}

## some triks for size

CMD cp $PR/../files/user_squash.env $OWTMP/boot/user.env
CMD cp $PR/../files/.auto* $OWTMP

case $BOARD in
    VIM3*)

#    [ "$ITYPE" = "emmc" ] && {
#	echo "[i] BOOT FROM SD">&2
#        CMD sed -i s/mmcblk0/mmcblk2/ $OWTMP/boot/user.env
#    }
#
#    [ "$ITYPE" = "sd" ] && {
#	echo "[i] BOOT FROM SD">&2
#        CMD sed -i s/mmcblk0/mmcblk1/ $OWTMP/boot/user.env
#    }

    ;;

    *)

#    [ "$ITYPE" = "emmc" ] && {
#	echo "[i] BOOT FROM EMMC">&2
#    
#        CMD sed -i s/mmcblk0/mmcblk1/ $OWTMP/boot/user.env
#    }

    ;;
esac

CMD cp $PR/../files/docs/* $OWTMP
CMD cp -a $PR/../files/scripts $OWTMP
#CMD cp $PR/../files/docs/* $OWTMP
CMD cp $PR/../README.changes.md $OWTMP

echo "[i] UBOOT WRITE $UBOOT_BIN">&2

CMD dd if=/dev/zero bs=512 count=$P1OFFSET of=$IMG_VFAT
CMD dd if=$UBOOT_BIN of=$IMG_VFAT conv=fsync,notrunc bs=444 count=1
CMD dd if=$UBOOT_BIN of=$IMG_VFAT conv=fsync,notrunc bs=512 skip=1 seek=1

F="${IMG_VFAT}.vfat"

CMD dd if=/dev/zero bs=1M count=$PART0SIZE of=$F
CMD mkfs.vfat -n $PART1_LABEL -i DEADBEEF $F


P=$(realpath $PWD)
#F="${IMG_VFAT}@@$((1+$PART0SIZE))M"

[ -d $IMG_VFAT.boot ] && \
    rm -rf $IMG_VFAT.boot

mkdir -p $IMG_VFAT.boot

CMD mdir -i $F ::
#CMD mdir -i $F ::boot
cd $OWTMP
for f in boot splash.* LABEL OPENWRT.label s9* aml* boot.* scripts README* .automount.no ; do
ls -l1 $f
CMD mcopy -s -i $F $f ::$f
CMD cp -a $f $IMG_VFAT.boot
done

CMD mdir -/ -i $F ::

#exit 0

cd $P

[ "$MAKE_KRESQ_IMAGE" ] && {
    echo ""
}

#echo no | mcat -i $F ::.automount.no

cat ${IMG_VFAT}.vfat >> $IMG_VFAT
CMD rm ${IMG_VFAT}.vfat

CMD sync

SFDISK_ARGS=-q
SFDISK_ARGS="-f --no-reread --no-tell-kernel"
#SFDISK_ARGS="-f --no-reread "

echo "[i] pake parts">&2
echo "label:dos | $sfdisk $SFDISK_ARGS $IMG_VFAT ">&2
echo "label:dos" | $sfdisk $SFDISK_ARGS $IMG_VFAT || FAIL

echo "[i] pake part 1">&2
echo "$P1OFFSET,  | $sfdisk $SFDISK_ARGS $IMG_VFAT -a">&2
echo "$P1OFFSET, " | $sfdisk $SFDISK_ARGS $IMG_VFAT -a || FAIL

echo "sfdisk $SFDISK_ARGS --part-type $IMG_VFAT 1 e">&2
$sfdisk $SFDISK_ARGS --part-type $IMG_VFAT 1 e || FAIL

[ "$COMP" = "" ] && COMP=gzip
#[ "$CMP_BLOCK_SIZE" ] || CMP_BLOCK_SIZE=1024k
[ "$CMP_BLOCK_SIZE" ] || CMP_BLOCK_SIZE=128k
[ "$COMP" = "xz" ] && CMP_OPTS="-Xdict-size $CMP_BLOCK_SIZE -Xbcj arm"
[ "$COMP" = "xz" ] && CMP_OPTS="-Xdict-size $CMP_BLOCK_SIZE"

[ -f $OWTMP.squashfs ] && rm $OWTMP.squashfs

CMD rm -rf $OWTMP/boot
CMD mkdir  $OWTMP/boot
CMD rm  $OWTMP/s9*
CMD rm  $OWTMP/splash*
CMD rm  $OWTMP/aml_*
[ -f $OWTMP/boot.ini ] && \
CMD rm  $OWTMP/boot.*
CMD rm  $OWTMP/LABEL
CMD rm  $OWTMP/*.label
CMD rm -rf $OWTMP/scripts

[ $COMP = "zstd" ] && {
    $mksquashfs 2>&1 | grep -q -m1 zstd || COMP=gzip
}

[ $COMP = "gzip" ] && {
    CMP_OPTS="$CMP_OPTS_GZIP"
}

[ $COMP = "zstd" ] && {
    CMP_OPTS="$CMP_OPTS_ZSTD"
}

CMD $mksquashfs \
    "$OWTMP" \
    $OWTMP.squashfs \
    -all-root \
    -b $CMP_BLOCK_SIZE \
    -comp $COMP $CMP_OPTS || FAIL

S=$(stat -c%s $IMG_VFAT)

#cat $OWTMP.squashfs >> $IMG_VFAT

SQ=$(stat -c%s $OWTMP.squashfs)

# overlay padded offset calc
SQQ=$((SQ/65536*65536))
[ "$SQQ" -lt "$SQ" ] && SQQ=$((SQQ+65536))

CMD dd if=/dev/zero bs=1M count=$PART2SIZE of=$IMG_VFAT.overlay
CMD dd if=$OWTMP.squashfs bs=1M conv=sync,notrunc of=$IMG_VFAT.overlay

[ "$NO_REMOVE_SQUASH" ] || \
    CMD rm $OWTMP.squashfs

[ "$OVERLAY_INIT" ] && {

    #/dev/loop0: LABEL="rootfs_data" UUID="006fb1a0-7c35-4515-80f0-889d7a8a1ca1" TYPE="ext4"
    #root@openwrt-vim:/# ls -l1 /overlay/
    #drwxr-xr-x    3 root     root          1024 Jan  1  1970 upper
    #drwxr-xr-x    4 root     root          1024 Jan  1  1970 work
    TMP_OVL=/tmp/openwrt.ovl
    mkdir -p $TMP_OVL || FAIL
    mkdir -p $TMP_OVL/upper $TMP_OVL/work || FAIL
    echo "overlay created $DATE" > $TMP_OVL/.OVERLAY
    echo "overlay created $DATE" > $TMP_OVL/upper/.overlay
    ln -s 2 $TMP_OVL/.fs_state

    echo "[i] INIT OVERLAY as ext4">&2
    ARGS=
    # ARGS="-U 12345678-dead-beef-0000-000011112222"

    CMD mkfs.ext4 -F -d $TMP_OVL -E offset=$SQQ -L $PART2_1_LABEL $ARGS $IMG_VFAT.overlay || FAIL


    rm -rf $TMP_OVL


}

CMD sync

#BS=4096
#mkdir /tmp/_empty
#echo "OPENWRT_USER_JFFS2" > /tmp/_empty/LABEL
#CMD mkfs.jffs2 -r /tmp/_empty -s $BS -X lzo -q -p $((1000*$BS)) -o $IMG_VFAT.overlay
#rm -rf /tmp/_empty

CMD ls -l1 $IMG_VFAT.overlay

#CMD dd if=/tmp/image.jffs2 of=$IMG_VFAT.overlay conv=fsync,notrunc

cat $IMG_VFAT.overlay >> $IMG_VFAT
rm $IMG_VFAT.overlay

#dd if=/dev/zero bs=1M count=$PART2SIZE >> $IMG_VFAT

CMD sync

echo "[i] echo $P1OFFSET,  | sfdisk -q $IMG_VFAT -a || FAIL">&2
echo "$P1OFFSET, " | $sfdisk -q $IMG_VFAT -a || FAIL


IMG=${IMG_VFAT%.img}

[ "$MKZIP" ] && {
IMGZ=$IMG.zip

$PR/opkg list-installed | grep -v kmod- > /tmp/openwrt-pre-installed-packages.txt

[ -f $IMG ] || rm $IMGZ

CMD zip -1 -j $IMGZ \
    $IMG_VFAT \
    $IMG_VFAT.md5sum \
    $PR/../README*txt \
    /tmp/openwrt-pre-installed-packages.txt
}

#cat $PR/../README.openwrt.vims.txt \
#    $PR/../README.changes.txt \
#    > $IMG_.readme.txt

    (
    head -n 32 $PR/../README.changes.txt
    echo "...."
    cat $PR/../files/docs/README.md
    ) > $IMG_.README.txt


    ln -s $IMG_NAME2.README.txt $IMG_LAST.README.txt

(
[ "$MAKE_KRESQ_IMAGE" ] && {

[ "$KRESQ_PATH" ] || KRESQ_PATH=/tmp/openwrt_kresq
[ -d "$KRESQ_PATH" ] && rm -rf $KRESQ_PATH
[ -d "$KRESQ_PATH" ] || mkdir  $KRESQ_PATH

echo "[i] KRESQ for $BOARDS">&2
for B in $BOARDS; do
DL $UBOOT_DL/$UBOOT_REL/$B.u-boot.sd.bin || FAIL
CMD cp $R $IMG_VFAT.boot/boot
done
CMD mv $IMG_VFAT.boot $KRESQ_PATH/OW_BOOT

    CMD mkdir $KRESQ_PATH/OW_DATA
    CMD cp $OWTMP.squashfs $KRESQ_PATH/OW_DATA
    CMD mkdir $KRESQ_PATH/.meta

    (
    head -n 32 $PR/../README.changes.txt
    echo "...."
    cat $PR/../files/docs/README.md
    ) > $KRESQ_PATH/README.txt

    export DATE="$DATE"
    export LABEL="$LABEL"
    export KRESQ_IMAGE_NAME="$IMG_NAME2"
    export KRESQ_IMAGE_DESC="$LABEL $REL $VER $apps"

    sh $PR/../files/krescue.image.conf.tpl > $KRESQ_PATH/.krescue.image.conf

    make_image=$(which make_image)

    (
    cd $KRESQ_PATH
    $make_image
    ) && ln -sf $IMG_NAME2.emmc.kresq $IMG_LAST.emmc.kresq

#ln -sf $IMG_

}
)

ls -l1 

#md5sum $IMG_VFAT > $IMG_VFAT.md5sum
gzip -1 -c $IMG_VFAT > $IMG_VFAT.gz


[ "$MAKE_ALL" ] && {

echo "[i] make all $BOARDS">&2
for B in $BOARDS; do

IMG2_="$B.$IMG_CNAME.img"
IMG2="$OUT_/$B.$IMG_CNAME.img"
IMG3="$OUT_/$B.$IMG_CNAML_.last.img"

ln -sf $IMG2_.gz $IMG3.gz

[ "$B" = "$BOARD" ] && continue

DL https://github.com/hyphop/khadas-uboot/releases/download/$UBOOT_REL/$B.u-boot.sd.bin || FAIL

CMD cp $IMG_VFAT $IMG2
CMD dd if=/dev/zero of=$IMG2 conv=fsync,notrunc bs=1M count=1 seek=1	2>/dev/null	|| FAIL  mkboot1
CMD dd if=$R of=$IMG2 conv=fsync,notrunc bs=444 count=1			2>/dev/null	|| FAIL  mkboot2
CMD dd if=$R of=$IMG2 conv=fsync,notrunc bs=512 skip=1 seek=1		2>/dev/null	|| FAIL  mkboot3

#md5sum $IMG2 > $IMG2.md5sum
gzip -1 -c $IMG2 > $IMG2.gz
#ln -sf $IMG2_.gz $IMG3.gz

done


}

#NO_RAW_IMG=1
(
cd $OUT_
md5sum   *.$IMG_CNAME.img \
	 *.$IMG_CNAME.img.gz \
	 *.$IMG_NAME2.kresq \
      > $IMG_NAME2.md5sums
)

[ "$NO_RAW_IMG" ] && {
for r in $OUT_/*.$IMG_CNAME.img; do
    echo "[i] clean raw image $r"
    rm $r
done

}

echo "[i] DONE">&2


ls -l1 $OUT_/*.$IMG_CNAME.*



